package com.dimong.www.loan.ui.frag.main;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.loadmore.SwipeRefreshHelper;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.dimong.loan.sdk.net.NetHttpCallback;
import com.dimong.loan.sdk.ui.adapter.BaseRecyclerAdapter;
import com.dimong.www.loan.R;
import com.dimong.www.loan.app.MyApplication;
import com.dimong.www.loan.bean.ClassifyBean;
import com.dimong.www.loan.bean.FocusBean;
import com.dimong.www.loan.bean.MainBean;
import com.dimong.www.loan.bean.ProdBean;
import com.dimong.www.loan.net.LoadHttpUtil;
import com.dimong.www.loan.net.response.prod.GetIndexData;
import com.dimong.www.loan.ui.UIHelper;
import com.dimong.www.loan.ui.adapter.holder.CarouselPicHolder;
import com.dimong.www.loan.ui.adapter.holder.LoadRecommendHolder;
import com.dimong.www.loan.ui.adapter.main.MainAdapter;
import com.dimong.www.loan.ui.base.BaseRefreshFragment;
import com.dimong.www.loan.widget.LoanRefreshView;

import java.util.List;

public class MainOneFrag extends BaseRefreshFragment implements LoadRecommendHolder.LoadRecommendHolderInterface, CarouselPicHolder.CarouselPicHolderInterface {
    private SwipeRefreshLayout srl_frag_main_one;
    private RecyclerView rv_frag_main_one;
    private MainAdapter mainAdapter;
    private SwipeRefreshHelper mSwipeRefreshHelper;

    @Override
    public void beforeInitView() {

    }

    @Override
    public void initView() {
        super.initView();
        srl_frag_main_one = (SwipeRefreshLayout) findViewById(R.id.srl_frag_main_one);
        rv_frag_main_one = (RecyclerView) findViewById(R.id.rv_frag_main_one);
        rv_frag_main_one.setItemAnimator(null);
    }

    @Override
    public void setViewStatus() {
        rv_frag_main_one.setLayoutManager(new LinearLayoutManager(getActivity()));
        mainAdapter = new MainAdapter();
        mainAdapter.setLoadRecommendHolderInterface(this);
        mainAdapter.setCarouselPicHolderInterface(this);
        rv_frag_main_one.setAdapter(new RecyclerAdapterWithHF(mainAdapter));

        mSwipeRefreshHelper = new SwipeRefreshHelper(srl_frag_main_one, rv_frag_main_one);
        mSwipeRefreshHelper.setLoadMoreEnable(false);
        mSwipeRefreshHelper.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                getData();
            }
        });
        mSwipeRefreshHelper.setOnSwipeRefreshListener(new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onfresh() {
                getData();
            }
        });

        mainAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                MainBean mainBean = (MainBean) mainAdapter.getItem(position);
                if (mainBean.getObject() instanceof ProdBean) {
                    ProdBean prodBean = (ProdBean) mainBean.getObject();
                    UIHelper.gotoProdDetailed(getActivity(), prodBean.getProd_id());
                }
            }
        });
        srl_frag_main_one.setVisibility(View.GONE);

        setLoanReloadClick(new LoanRefreshView.LoanReloadClick() {
            @Override
            public void onRefreshClick() {
                showRefreshLoading();
                getData();
            }
        });
        showRefreshLoading();
        getData();
    }

    @Override
    public int getViewResourceId() {
        return R.layout.frag_main_one;
    }

    public void getData() {
        LoadHttpUtil.getIndexData(new NetHttpCallback() {
            @Override
            public void onResult(String result) {
                mSwipeRefreshHelper.refreshComplete();
                super.onResult(result);
                GetIndexData getIndexData = gson.fromJson(result, GetIndexData.class);
                showRefreshSuccess();
                srl_frag_main_one.setVisibility(View.VISIBLE);
                setData(getIndexData);
            }

            @Override
            public void onError(String errorCode, String error) {
                super.onError(errorCode, error);
                mSwipeRefreshHelper.refreshComplete();
                showRefreshError();
                showToast(error);
            }
        });
    }

    private boolean isAddRecommendHead;

    private void setData(GetIndexData getIndexData) {
        mainAdapter.clear();
        isAddRecommendHead = false;
        if (getIndexData.getFocus() != null && getIndexData.getFocus().size() != 0) {
            MainBean bean = new MainBean();
            bean.setType(MainAdapter.ITEM_CAROUSEL_PIC);
            bean.setObject(getIndexData.getFocus());
            mainAdapter.addData(bean);
        }

        if (getIndexData.getClassify() != null) {
            MainBean bean = new MainBean();
            bean.setType(MainAdapter.ITEM_LOAD_RECOMMEND);
            bean.setObject(getIndexData.getClassify());
            mainAdapter.addData(bean);
        }

        if (getIndexData.getHot() != null && getIndexData.getHot().size() != 0) {
            if (!isAddRecommendHead) {
                isAddRecommendHead = true;
                MainBean bean = new MainBean();
                bean.setType(MainAdapter.ITEM_HOT_RECOMMEND_HEAD);
                mainAdapter.addData(bean);
            }
            mSwipeRefreshHelper.setLoadMoreEnable(false);
            for (int i = 0; i < getIndexData.getHot().size(); i++) {
                MainBean bean1 = new MainBean();
                bean1.setType(MainAdapter.ITEM_HOT_RECOMMEND);
                bean1.setObject(getIndexData.getHot().get(i));
                mainAdapter.addData(bean1);
            }
        } else {
            mSwipeRefreshHelper.setLoadMoreEnable(false);
        }

        mainAdapter.updateChange();
    }

    @Override
    public void onLoadRecommendHolderClick(int adapterPos, int pos) {
        MainBean mainBean = (MainBean) mainAdapter.getItem(adapterPos);
        if (mainBean.getObject() instanceof List) {
            List<ClassifyBean> classifyBeans = (List<ClassifyBean>) mainBean.getObject();
            ClassifyBean classifyBean = classifyBeans.get(pos);
            UIHelper.gotoProdListAty(getActivity(), classifyBean.getClassify_name(), classifyBean.getClassify_id());
        }
    }

    @Override
    public void onCarouselPicHolderClick(int adapterPos, int pos) {
        MainBean mainBean = (MainBean) mainAdapter.getItem(adapterPos);
        if (mainBean.getObject() instanceof List) {
            List<FocusBean> focusBeans = (List<FocusBean>) mainBean.getObject();
            FocusBean focusBean = focusBeans.get(pos);
            if ("1".equals(focusBean.getJump_type())) {
                if (MyApplication.isLogin()) {
                    LoadHttpUtil.subLoanLog("1",focusBean.getId(), "", "", new NetHttpCallback() {
                        @Override
                        public void onPreRequest() {
                            super.onPreRequest();
                            showRefreshLoading(true);
                        }

                        @Override
                        public void onResult(String result) {
                            super.onResult(result);
                            showRefreshSuccess();
                            ProdBean prodBean = gson.fromJson(result, ProdBean.class);
                            UIHelper.openWebViewAty(getActivity(), prodBean.getLoan_name(), prodBean.getLink());
                        }

                        @Override
                        public void onError(String errorCode, String error) {
                            showRefreshSuccess();
                            super.onError(errorCode, error);
                        }
                    });
                } else {
                    MyApplication.openLogin();
                }
            }
            if ("2".equals(focusBean.getJump_type())) {
                UIHelper.gotoProdDetailed(getActivity(), focusBean.getProd_id());
            }
        }
    }

    @Override
    public RelativeLayout getContainView() {
        return (RelativeLayout) findViewById(R.id.rl_frag_main_one);
    }
}
